*************************************************
*Figure A2: Robot exposure by industry exclusion*
*************************************************

*Education
use "$final_data_outcomes/czone_Students.dta", clear
merge 1:1 czone year using "$final_data_outcomes/IPEDS_EnrollmentCZ.dta", assert(1 3) nogenerate

*Automation
merge 1:1 czone year using "$final_data_automation/czones_ExposureAutomation.dta", assert(1 3) nogenerate
merge 1:1 czone year using "$final_data_automation/czones_AutomationExposureIndustry.dta", assert(3) nogenerate

*Imports
merge 1:1 czone year using "$final_data_imports/czone_ExposureChina", assert(1 3) nogenerate

*Covariates
merge m:1 czone using "$final_data_covariates/czone1990_Covariates.dta", assert(3) nogenerate //beginning of period
merge m:1 czone using "$final_data_covariates/IPEDS_institutionControlsCZ.dta", keep(1 3) nogenerate

xtset czone year

*Keep only periods of interest
keep if year<=2000

foreach var in public communityCollege uniTop20 uniTop30Dummy uniTop30 largest150 forProfit nonProfit numberOfUni private {
replace `var' = 0 if mi(`var')
}

egen medianAnyAid = median(anyAid), by(statefip)
	replace anyAid = medianAnyAid if mi(anyAid)
	
*Coefficients and CI
foreach var in car elc fod frn mot mbs mmc mpr mnr ppr ptr txt vho cst mng rsc ser agr uti { 
gen coef_`var' =.
gen se_`var' =.
gen ci_u_`var' =.
gen ci_d_`var' =.
}

foreach var in car elc fod frn mot mbs mmc mpr mnr ppr ptr txt vho cst mng rsc ser agr uti { 
local i = 1

reghdfe d_sc expo_`var'_euro7_qo expon_`var'_euro7_qo d_exp_iv_stacked d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

*Use set of industries excluding one at a time
replace coef_`var' = _b[expon_`var'_euro7_qo] in `i'
replace se_`var' = _se[expon_`var'_euro7_qo] in `i'
replace ci_u_`var' = coef_`var'+1.96*se_`var' in `i'
replace ci_d_`var' = coef_`var'-1.96*se_`var' in `i'
}

keep if _n==1
keep statefip coef* se* ci*

reshape long coef_ se_ ci_u_ ci_d_, i(statefip) j(industry) string

sort industry

gen id = _n

gen industry_name = ""
replace industry_name = "Agriculture" if industry=="agr"
replace industry_name = "Automotive" if industry=="car"
replace industry_name = "Electronics" if industry=="mbs"
replace industry_name = "Basic Metals" if industry=="elc"
replace industry_name = "Food and Beverages" if industry=="fod"
replace industry_name = "Industrial Machinery" if industry=="mmc"
replace industry_name = "Metal products" if industry=="mpr"
replace industry_name = "Minerals" if industry=="mnr"
replace industry_name = "Miscellaneous Manufacturing" if industry=="mot"
replace industry_name = "Paper and Printing" if industry=="ppr"
replace industry_name = "Plastics and Chemicals" if industry=="ptr"
replace industry_name = "Shipbuilding and Aerospace" if industry=="vho"
replace industry_name = "Textiles" if industry=="txt"
replace industry_name = "Wood and Furniture" if industry=="frn"
replace industry_name = "Construction" if industry=="cst"
replace industry_name = "Education and Research" if industry=="rsc"
replace industry_name = "Mining" if industry=="mng"
replace industry_name = "Services" if industry=="ser"
replace industry_name = "Utilities" if industry=="uti"

gen industry_non_man = (industry=="agr"|industry=="cst"|industry=="rsc"|industry=="mng"|industry=="ser"|industry=="uti")
gen industry_man = (industry_non_man==1)

sort industry_man industry_name

encode industry_name, gen(industry_id) label(industry_name)

twoway  (bar coef_ industry_id, barwidth(0.7) fcolor(navy)  lcolor(black) lwidth(vthin)) ///
		 (rcap ci_u_ ci_d_ industry_id, lcolor(black) lwidth(vthin)) (scatter coef_ industry_id, color(black) msymbol(diamond) msize(small)) ///
		, ylabel(-.2(.15).7, angle(horiz) labsize(large) grid gmin gmax format(%5.2f)) yline(0, lcolor(black) lwidth(vthin)) xtitle("")  ///
		legend(off) ysize(2) ytitle("US robot exposure" "point estimate", size(large)) ///
		xscale(noline) xlabel(1(1)19, valuelabel labsize(large) angle(45) noticks) ///
		graphregion(color(white) fcolor(white) margin(r+20)) 
		graph export "$figures_appendix/Figure A4.png", replace







